package org.lds.ers.client.widgets;

import org.lds.ers.client.LoginService;
import org.lds.ers.client.LoginServiceAsync;
import org.lds.ers.client.MyInfo;
import org.lds.ers.client.StakeService;
import org.lds.ers.client.StakeServiceAsync;
import org.lds.ers.client.model.LoginInfo;
import org.lds.ers.client.model.StakeInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MaintainStakeSForm extends DialogBox {
	
	private StakeInfo stake;
	
	private final VerticalPanel pnlMain;
	private final Label lblMsg;
	private final DialogBox self;
	private final TextBox txtStakeName;
	private final TextBox txtStakeNo;
	private final TextBox txtRemark;
	
	private StakeServiceAsync stakeService = GWT.create(StakeService.class);
	
	public MaintainStakeSForm()
	{
		this.self = this;
	
		this.pnlMain = new VerticalPanel();
		this.pnlMain.setWidth("400px");
		this.add(pnlMain);

		this.lblMsg = new Label("新增一個支聯會資料：");
		this.pnlMain.add(lblMsg);
		
		this.txtStakeName = new TextBox();
		this.txtStakeNo = new TextBox();
		this.txtRemark = new TextBox();
		
		addContentControls();
		
		addButton();
	}
	
	private void addContentControls()
	{
		//1. 加入 Label 和 TextBox  for StakeName
		HorizontalPanel pnl1 = new HorizontalPanel();
		pnl1.add(new Label("支聯會名稱："));
		pnl1.add(this.txtStakeName);
		this.pnlMain.add(pnl1);
		//2. 加入 Label 和 TextBox  for StakeNo
		HorizontalPanel pnl2 = new HorizontalPanel();
		pnl2.add(new Label("支聯會編號："));
		pnl2.add(this.txtStakeNo);
		this.pnlMain.add(pnl2);
		//3. 加入 Label 和 TextBox  for Remark
		HorizontalPanel pnl3 = new HorizontalPanel();
		pnl3.add(new Label("備註："));
		pnl3.add(this.txtRemark);
		this.pnlMain.add(pnl3);
		
	}
	
	private void addButton()
	{
		final HorizontalPanel pnl = new HorizontalPanel();
		this.pnlMain.add(pnl);
		this.pnlMain.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		final Button btnSave = new Button("儲存");
		btnSave.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				btnSave.setEnabled(false);
				if (stake == null)	//新增
				{
					StakeInfo s = new StakeInfo();
					s.setStakeName(txtStakeName.getText());
					s.setStakeNo(txtStakeNo.getText());
					s.setRemark(txtRemark.getText());
					s.setByWhom(MyInfo.get().getEmailAddress());
					stakeService.insert(s, new AsyncCallback<StakeInfo>() {
						public void onFailure(Throwable caught) {
							Window.alert("儲存失敗");
							btnSave.setEnabled(true);
						}
			
						public void onSuccess(StakeInfo result) {
							self.hide();
							btnSave.setEnabled(true);
							MaintainStake.getInstance().refreshTable();
						}
					});
				}
				else {	//修改
					stake.setStakeName(txtStakeName.getText());
					stake.setStakeNo(txtStakeNo.getText());
					stake.setRemark(txtRemark.getText());
					stake.setByWhom(MyInfo.get().getEmailAddress());
					stakeService.update(stake, new AsyncCallback<Boolean>() {
						public void onFailure(Throwable caught) {
							Window.alert("儲存失敗");
						}
			
						public void onSuccess(Boolean result) {
							self.hide();
							MaintainStake.getInstance().refreshTable();
						}
					});
				}
			}
		});
		pnl.add(btnSave);
		
		pnl.setCellHorizontalAlignment(btnSave, HasHorizontalAlignment.ALIGN_CENTER);

		final Button btnClose = new Button("取消");
		btnClose.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				self.hide();
			}
		});
		pnl.add(btnClose);
		
		pnl.setCellHorizontalAlignment(btnClose, HasHorizontalAlignment.ALIGN_CENTER);
	}
	
	public void setStakeInfo(StakeInfo stake)
	{
		this.stake = stake ;
		if (stake !=null)
		{
			this.lblMsg.setText("修改 " + stake.getStakeName());
			this.txtStakeName.setText(stake.getStakeName());
			this.txtStakeNo.setText(stake.getStakeNo());
			this.txtRemark.setText(stake.getRemark());
		}
	}
	
}
